// pages/user/feedback.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    feedbackType: '',
    content: '',
    contentLength: 0,
    images: [],
    contact: '',
    submitDisabled: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 如果用户已登录，自动填充联系方式
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo && userInfo.phone) {
      this.setData({
        contact: userInfo.phone
      });
    }
  },

  /**
   * 选择反馈类型
   */
  selectType(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      feedbackType: type
    });
    this.checkSubmitButton();
  },

  /**
   * 输入反馈内容
   */
  inputContent(e) {
    const content = e.detail.value;
    this.setData({
      content: content,
      contentLength: content.length
    });
    this.checkSubmitButton();
  },

  /**
   * 输入联系方式
   */
  inputContact(e) {
    this.setData({
      contact: e.detail.value
    });
  },

  /**
   * 选择图片
   */
  chooseImage() {
    const { images } = this.data;
    const remainCount = 3 - images.length;
    
    if (remainCount <= 0) {
      return;
    }
    
    wx.chooseImage({
      count: remainCount,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // 将选择的图片添加到图片数组
        this.setData({
          images: [...images, ...res.tempFilePaths]
        });
      }
    });
  },

  /**
   * 预览图片
   */
  previewImage(e) {
    const index = e.currentTarget.dataset.index;
    const { images } = this.data;
    
    wx.previewImage({
      current: images[index],
      urls: images
    });
  },

  /**
   * 删除图片
   */
  deleteImage(e) {
    const index = e.currentTarget.dataset.index;
    const { images } = this.data;
    
    images.splice(index, 1);
    this.setData({
      images
    });
  },

  /**
   * 检查提交按钮状态
   */
  checkSubmitButton() {
    const { feedbackType, content } = this.data;
    
    this.setData({
      submitDisabled: !feedbackType || !content.trim()
    });
  },

  /**
   * 提交反馈
   */
  submitFeedback() {
    const { feedbackType, content, images, contact } = this.data;
    
    if (!feedbackType) {
      wx.showToast({
        title: '请选择反馈类型',
        icon: 'none'
      });
      return;
    }
    
    if (!content.trim()) {
      wx.showToast({
        title: '请输入反馈内容',
        icon: 'none'
      });
      return;
    }
    
    // 显示加载提示
    wx.showLoading({
      title: '提交中...',
      mask: true
    });
    
    // 模拟上传图片和提交反馈的过程
    setTimeout(() => {
      wx.hideLoading();
      
      // 显示提交成功提示
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          // 延迟返回上一页
          setTimeout(() => {
            wx.navigateBack();
          }, 2000);
        }
      });
      
      // 实际开发中，这里应该调用API提交反馈信息
      console.log('提交的反馈信息：', {
        type: feedbackType,
        content: content,
        imageCount: images.length,
        contact: contact
      });
    }, 1500);
  }
})